package com.example.ormsqlsplicingframe1.sql.impl;

import com.example.ormsqlsplicingframe1.sql.FieldSql;
import com.example.ormsqlsplicingframe1.utlis.ObjValue;

import java.util.*;
import java.util.stream.Collectors;

public class FieldsSql  implements FieldSql{


    private Map<String,Object> map  = new  LinkedHashMap();

    public FieldsSql field(String name,Object value){
        if (ObjValue.isNotNull(value)){
            map.put(name,value);
        }

        return this;
    }

    public FieldsSql inc(String name,Number value){
        map.put(name+" = "+name+"+ ?",value);

        return this;
    }



    public String getInsertSql(){

        //  (name,age)  value (?,?)

        final StringJoiner fields = new StringJoiner(" , ","(",")");

        StringJoiner values = new StringJoiner(" , ","(",")");

        for (String s : map.keySet()) {
            final Object o = map.get(s);
            fields.add(s);
            values.add("?");

        }
       return fields+" value "+values;

    }


    public String getUpdateSql(){

        // nage = ? , age =?
        final StringJoiner fields = new StringJoiner(" , ");

        for (String s : map.keySet()) {
            if(s.endsWith("?")){
                fields.add(s);
            }else {
                fields.add(s+" = ? ");
            }

        }

        return fields.toString();

    }

    public List<Object> getValues(){
       return map.values().stream().collect(Collectors.toList());
    }

}

